﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.DAO.委托;
using LibrarySolution.DAO.基础;
using LibrarySolution.ComMethod;
using LibrarySolution.Model.基础;
using Microsoft.Reporting.WinForms;
using LibrarySolution.Frm.报表;

namespace LibrarySolution.Frm
{
    public partial class FrmYhxx : Form
    {
        #region 属性
        Delegates delegates = new Delegates();
        private static FrmYhxx Instance;
        YhDao yhxxDao = new YhDao();
        String yhNo;
        String yhName;
        String yhState;
        #endregion

        #region 构造器
        public FrmYhxx()
        {
            InitializeComponent();
            delegates.refreshLBEvent += new Delegates.refreshLBEventHandler(delegates_refreshLBEvent);
            refreshDtgv();
        }
        #endregion

        #region 事件
        public static FrmYhxx GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
            {
                Instance = new FrmYhxx();
                Instance.MdiParent = GlobalVar.frmMain;
                GlobalVar.frmMain.panel1.Controls.Add(Instance);
            }
            if (String.IsNullOrEmpty(Instance.yhNo)) Instance.toolStrip1.Items["TstripBtnLos"].Enabled = false;
            Instance.BringToFront();
            return Instance;
        }

        private void TxtQueryByYhNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                YhxxEntity yhxxEntity = new YhxxEntity();
                yhxxEntity.yh_no = TxtQueryByYhNo.Text.Trim();
                DataTable dt = yhxxDao.getDTbyYhNo(yhxxEntity);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或用户不存在!");
                else
                {
                    dataGridView1.DataSource = dt;
                    dataGridView1.Refresh();
                }
                TxtQueryByYhNo.Text = "";
            }
        }

        private void TxtQueryByYhName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                YhxxEntity yhxxEntity = new YhxxEntity();
                yhxxEntity.yh_name = TxtQueryByYhName.Text.Trim();
                DataTable dt = yhxxDao.getDTbyYhName(yhxxEntity);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或用户不存在!");
                else
                {
                    dataGridView1.DataSource = dt;
                    dataGridView1.Refresh();
                }
                TxtQueryByYhName.Text = "";
            }
        }

        private void TstripBtnRef_Click(object sender, EventArgs e)
        {
            refreshDtgv();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            YhxxEntity yhxxEntity = null;
            String yhNo, yhState;
            yhNo = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            yhState = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[10].Value.ToString();
            yhxxEntity = yhxxDao.getYhxxByNo(yhNo, "0");
            GlobalVar.YhxxEntity = yhxxEntity;
            FrmYhxxModify.IsModify = true;
            FrmYhxxModify.GetInstance(delegates).ShowDialog();
        }

        private void TtripBtnAdd_Click(object sender, EventArgs e)
        {
            FrmYhxxModify.IsModify = false;
            FrmYhxxModify.GetInstance(delegates).ShowDialog();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                if (e.Value != null && e.Value.ToString().Length > 0)
                    e.Value = new string('*', e.Value.ToString().Length);
            }
        }

        private void TstripBb_Click(object sender, EventArgs e)
        {
            String time = DataUtil.getTime();
            YhxxEntity yhxxEntity = new YhxxEntity();
            String path = GlobalVar.path + @"\Frm\报表\YhReport.rdlc";
            ReportDataSource Rds = new ReportDataSource("DataSet", yhxxDao.getYhBb(yhxxEntity));
            FrmBb.GetInstance(Rds, path, time).ShowDialog();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("标记用户: "+yhName+"("+yhNo+") 为丢失?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                yhxxDao.MarkYh(yhNo,yhState);
                refreshDtgv();
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            yhNo = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            yhName = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
            yhState = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[10].Value.ToString();
            if (yhState == "有效")
                this.toolStrip1.Items["TstripBtnLos"].Enabled = true;
            else
                this.toolStrip1.Items["TstripBtnLos"].Enabled = false;
        }

        private void TstripBtnDel_Click(object sender, EventArgs e)
        {
            String yhXx = yhName + "(" + yhNo + ")";
            if (MessageBox.Show("删除用户: " +yhXx+ "?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                yhxxDao.delSelectedRow(yhNo);
                MessageBox.Show("已经成功删除用户 " + yhXx, "提示");
                refreshDtgv();
            }
        }

        private void FrmYhxx_FormClosing(object sender, FormClosingEventArgs e)
        {
           // GlobalVar.YhEnter = false;
        }
        #endregion

        #region 方法
        void delegates_refreshLBEvent()
        {
            refreshDtgv();
        }

        void refreshDtgv()
        {
            DataTable dt = null;
            YhxxEntity yhxxEntity = new YhxxEntity();
            dt = yhxxDao.getYhDataTable(yhxxEntity);
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();
        }
        #endregion

    }
}
